﻿using System;
using System.Web;
using DayDayUp.Core;
using DayDayUp.Core.Domain.Users;
using DayDayUp.Service.Authentication;
using DayDayUp.Service.Users;

namespace DayDayUp
{
    public class WebWorkContext:IWorkContext
    {
        private IAuthenticationService _authenticationService;
        private User _cachedUser;
        private IUserService _userService;


        public WebWorkContext(IAuthenticationService authenticationService, 
            IUserService userService 
           )
        {
            _authenticationService = authenticationService;
            _userService = userService;
          
        }

        private const string UserCookieName = "ddup.user";

      
        public User CurrentUser
        {
            get
            {
                return GetCurrentUser();
            }
            set
            {
                SetCustomerCookie(value.UserGuid);
                _cachedUser = value;
            }
        }

        protected User GetCurrentUser()
        {

            if (_cachedUser != null)
                return _cachedUser;

            User user= null;
            if (HttpContext.Current != null)
            {

                //registered user
                if (user == null || user.Deleted || !user.Active)
                {
                    user = _authenticationService.GetAuthenticatedCustomer();
                }



                //load guest customer
                if (user == null || user.Deleted || !user.Active)
                {
                    var customerCookie = GetCustomerCookie();
                    if (customerCookie != null && !String.IsNullOrEmpty(customerCookie.Value))
                    {
                        Guid customerGuid = new Guid(customerCookie.Value);

                        var customerByCookie = _userService.GetUserByGuid(customerGuid);
                        if (customerByCookie != null &&
                            //this customer (from cookie) should not be registered
                            !customerByCookie.IsRegistered())
                            user = customerByCookie;
                    }
                }

                //create guest if not exists
                if (user == null || user.Deleted || !user.Active)
                {
                    user = _userService.InsertGuestUser();
                }

                SetCustomerCookie(user.UserGuid);
            }


            _cachedUser = user;
            return _cachedUser;

        }

        private void SetCustomerCookie(Guid userGuid)
        {
            var cookie = new HttpCookie(UserCookieName);
            cookie.Value = userGuid.ToString();
            if (userGuid == Guid.Empty)
            {
                cookie.Expires = DateTime.Now.AddMonths(-1);
            }
            else
            {
                int cookieExpires = 24 * 365; //TODO make configurable
                cookie.Expires = DateTime.Now.AddHours(cookieExpires);
            }
            var _httpContext = HttpContext.Current;

            if (_httpContext != null && _httpContext.Response != null)
            {
                _httpContext.Response.Cookies.Remove(UserCookieName);
                _httpContext.Response.Cookies.Add(cookie);
            }
        }

        private HttpCookie GetCustomerCookie()
        {
            if (HttpContext.Current == null || HttpContext.Current.Request == null)
                return null;

            return HttpContext.Current.Request.Cookies[UserCookieName];
        }
    }
}